/**
 * RegionServiceImpl.java
 * Created at 2017-2-8
 * Created by Administrator
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.model.Region;
import com.broadtext.rms.system.service.IRegionService;

/**
 * <p>
 * ClassName: RegionServiceImpl
 * </p>
 * <p>
 * Description: TODO
 * </p>
 * <p>
 * Author: Administrator
 * </p>
 * <p>
 * Date: 2017-2-8
 * </p>
 */
@Service("regionService")
public class RegionServiceImpl extends EntityServiceImpl<Region> implements IRegionService {
    /**
     * 
     * <p>
     * Description: 注解入相关DAO
     * </p>
     * 
     * @param entityDao
     */
    @Autowired
    @Qualifier("regionDao")
    public void setEntityDao(EntityDao<Region> entityDao) {
        super.entityDao = entityDao;
    }

    /**
     * 
     * <p>Description: 获取区域树</p>
     * @return 区域树列表
     * @throws DaoException dao异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Region> getRegionTree() throws DaoException {
        return this.entityDao.findByQuery("com.broadtext.rms.system.model.Region.findRegionTreeByQuery", null);
    }

    /**
     * <p>Description: 删除区域</p>
     * @param resIdArray 区域id
     * @param userName 操作人
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public void delRegion(String[] resIdArray, String userName) throws DaoException {
        if(StringUtils.isBlank(userName)){
        	return;
        }
    	for (String id : resIdArray) {
            Map<String, Object> queryMap;
            queryMap = new HashMap<String, Object>();
            queryMap.put("parentId", id);
            List<Region> list;
            //查出子目录 将子目录删除
            list = this.entityDao.findByQuery("com.broadtext.rms.system.model.Region.selectRegionByParentId", queryMap);
            for (Region region : list) {
                region.setDelFlag("1");
                region.setUpdateDate(new Date());
                region.setUpdateUser(userName);
                this.entityDao.updateRecord("com.broadtext.rms.system.model.Region.updateRecord", region);
            }
            Region region;
            region = this.entityDao.findEntityById("com.broadtext.rms.system.model.Region.findEntityById", id);
            if (region != null) {
                region.setDelFlag("1");
                region.setUpdateDate(new Date());
                region.setUpdateUser(userName);
                this.entityDao.updateRecord("com.broadtext.rms.system.model.Region.updateRecord", region);
            }
            
        }
    }

    /**
     * 提交相关的方法进行统计
     */
    @Override
    public String saveOrUpdateRegion(String record, String userName) throws DaoException {
    	
    	JSONObject jsonOb;
        jsonOb = JSON.parseObject(record);
        Region region;
        region = new Region();
        if (jsonOb.get("id") != null && !"".equals(jsonOb.get("id"))) {
            region = this.entityDao.findEntityById("com.broadtext.rms.system.model.Region.findEntityById", String.valueOf(jsonOb.get("id")));
            if (region == null) {
                return "error";
            }
        }
        if (jsonOb.get("regionName") != null && !"".equals(jsonOb.get("regionName"))) {
            region.setRegionName(String.valueOf(jsonOb.get("regionName")));
        }
        if (jsonOb.get("regionCode") != null && !"".equals(jsonOb.get("regionCode"))) {
            region.setRegionCode(String.valueOf(jsonOb.get("regionCode")));
        }
        if (jsonOb.get("regionType") != null && !"".equals(jsonOb.get("regionType"))) {
            region.setRegionType(String.valueOf(jsonOb.get("regionType")));
        }
        if (jsonOb.get("parentRegion") != null && !"".equals(jsonOb.get("parentRegion"))) {
            region.setParentRegion(String.valueOf(jsonOb.get("parentRegion")));
        }
        if (!this.judgeUnique(region)) {
            return "duplicate";
        }
        region.setUpdateDate(new Date());
        region.setUpdateUser(userName);
        if (region.getPkId() != null) {
          //修改的上级目录 是 原此目录的 子目录
            if (region.getPkId().equals(region.getParentRegion())) {
                return "parentRegion_error";
            }
            this.entityDao.updateRecord("com.broadtext.rms.system.model.Region.updateRecord", region);
        } else {
        	region.setPkId(UUIDUtils.getUUID());
            region.setDelFlag("0");
            region.setCreateDate(new Date());
            region.setCreateUser(userName);
            this.entityDao.insertRecord("com.broadtext.rms.system.model.Region.insertRecord", region);
        }
        return "success";
    }
    
    /**
     * <p>Description: 判断区域编码或区域名称是否唯一，同时要进行其他的计算</p>
     * @param region 区域对象
     * @return 判断标志
     */
    public Boolean judgeUnique(Region region) {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        
        //验证唯一性
        queryMap.put("regionCode", region.getRegionCode());
        queryMap.put("regionName", region.getRegionName());
        queryMap.put("id", region.getPkId());
        try {
            if (!this.entityDao.findByQuery("com.broadtext.rms.system.model.Region.judgeUnique",
                    queryMap).isEmpty()) {
                return false;
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return true;
    }

}
